package com.neulogistics.contrib.usercenter.api.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@ConfigurationProperties(prefix = "spring.redis")
@Configuration
public class RedissonConfig {
    private String host;
    private int port;
    private int database;
    private String password;

    @Bean(destroyMethod="shutdown")
    public RedissonClient redissonClient(){
        //1 创建配置
        Config config = new Config();
        String redisAddress = String.format("redis://%s:%s", host, port);
        config.useSingleServer()
                .setAddress(redisAddress)
                .setDatabase(database)
                .setPassword(password);
        //2.根据Config创建出RedissonClient
        return Redisson.create(config);
    }
}